package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.LetterAnnexSampleDao;
import com.leoman.entity.LetterAnnexSample;
import com.leoman.service.LetterAnnexSampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * LetterAnnexSampleServiceImpl
 * Created by 涂奕恒 on 2016/11/23 0023 下午 3:59.
 */
@Service
public class LetterAnnexSampleServiceImpl implements LetterAnnexSampleService {

    @Autowired
    private LetterAnnexSampleDao letterAnnexSampleDao;

    @Override
    public List<LetterAnnexSample> findAll() {
        return letterAnnexSampleDao.findAll();
    }

    @Override
    public Page<LetterAnnexSample> find(int pageNum, int pageSize) {
        return letterAnnexSampleDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<LetterAnnexSample> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public LetterAnnexSample getById(int id) {
        return letterAnnexSampleDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        letterAnnexSampleDao.delete(id);
    }

    @Override
    public LetterAnnexSample create(LetterAnnexSample letterAnnexSample) {
        return letterAnnexSampleDao.save(letterAnnexSample);
    }

    @Override
    public LetterAnnexSample update(LetterAnnexSample letterAnnexSample) {
        return letterAnnexSampleDao.save(letterAnnexSample);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public LetterAnnexSample findOneByLetterAnnexId(Integer letterAnnexId) {
        if (null == letterAnnexId) {
            return null;
        }
        return letterAnnexSampleDao.findOneByLetterAnnexId(letterAnnexId);
    }

    @Override
    public Page<LetterAnnexSample> iPageByParams(final Integer letterAnnexId, final Integer type, Integer pageNum, Integer pageSize) {
        Page<LetterAnnexSample> page = letterAnnexSampleDao.findAll(new Specification<LetterAnnexSample>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<LetterAnnexSample> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (null != letterAnnexId) {
                    predicateList.add(cb.equal(root.get("letterAnnexId").as(Integer.class), letterAnnexId));
                }

                if (null != type) {
                    predicateList.add(cb.equal(root.get("type").as(Integer.class), type));
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));

        for (LetterAnnexSample letterAnnexSample : page.getContent()) {
            letterAnnexSample.setId(letterAnnexSample.getLetterAnnexId());
        }

        return page;
    }
}